//
//  MYGMedicalToolsChooseView.swift
//  MYGrow
//
//  Created by 林矗 on 2024/3/10.
//

import UIKit

class MYGMedicalToolsChooseView: UIView {
    // MARK: - Property
    var selectedAction: ((Int) -> ())?
    var leftTitle = "" {
        didSet {
            leftButton.setTitle(leftTitle, for: .normal)
        }
    }
    
    var rightTitle = "" {
        didSet {
            rightButton.setTitle(rightTitle, for: .normal)
        }
    }
    
    var title = "" {
        didSet {
            titleLabel.text = title
        }
    }
    
    var selectedIndex: Int = 0 {
        didSet {
            if selectedIndex == oldValue {
                return
            }
            
            if selectedIndex == 0 {
                didSelectLeft()
            } else {
                didSelectRight()
            }
        }
    }
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        addSubviews([
            titleLabel,
            leftButton,
            rightButton
        ])
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.lessThanOrEqualTo(leftButton.snp.left).offset(-8)
            make.top.bottom.equalToSuperview().inset(16)
        }
        
        rightButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(0)
            make.size.equalTo(CGSize(width: 50.5, height: 21))
        }
        
        leftButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(rightButton.snp.left).offset(-12.5)
            make.size.equalTo(CGSize(width: 50.5, height: 21))
        }
    }
    
    // MARK: - Public funcs
    
    // MARK: - lazy load
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(51, 51, 51)
        label.text = "性别"
        label.numberOfLines = 0
        return label
    }()
    
    
    private lazy var leftButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("男", for: .normal)
        button.setTitleColor(.rgba(204, 204, 204), for: .normal)
        button.backgroundColor = .white
        button.titleLabel?.font = .regular(14)
        button.setTitleColor(.white, for: .selected)
        button.layerBorderWidth = 0.5
        button.layerBorderColor = .rgba(204, 204, 204)
        button.layerCornerRadius = 10.5
        button.addTarget(self, action: #selector(didSelectLeft), for: .touchUpInside)
        return button
    }()
    
    private lazy var rightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("女", for: .normal)
        button.setTitleColor(.rgba(204, 204, 204), for: .normal)
        button.backgroundColor = .white
        button.titleLabel?.font = .regular(14)
        button.setTitleColor(.white, for: .selected)
        button.layerBorderWidth = 0.5
        button.layerBorderColor = .rgba(204, 204, 204)
        button.layerCornerRadius = 10.5
        button.addTarget(self, action: #selector(didSelectRight), for: .touchUpInside)
        return button
    }()
}

extension MYGMedicalToolsChooseView {
    @objc private func didSelectLeft() {
        self.leftButton.isSelected = true
        self.leftButton.backgroundColor = .rgba(162, 146, 254)
        self.leftButton.layerBorderWidth = 0
        self.rightButton.backgroundColor = .white
        self.rightButton.layerBorderWidth = 0.5
        self.rightButton.isSelected = false
        selectedAction?(0)
        selectedIndex = 0
    }
    
    @objc private func didSelectRight() {
        self.rightButton.isSelected = true
        self.rightButton.backgroundColor = .rgba(162, 146, 254)
        self.rightButton.layerBorderWidth = 0
        self.leftButton.backgroundColor = .white
        self.leftButton.layerBorderWidth = 0.5
        self.leftButton.isSelected = false
        selectedAction?(1)
        selectedIndex = 1
    }
    
}
